/*
 * Copyright (C) 2013 The original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.zapta.apps.maniana.editors;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.zapta.apps.maniana.R;
import com.zapta.apps.maniana.annotations.MainActivityScope;
import com.zapta.apps.maniana.model.ItemModel;

/*
**
** @author Elliott Mitchell
**/
@MainActivityScope
abstract public class EditRepeat extends Fragment implements
DatePickerDialog.OnDateSetListener, TextWatcher, View.OnClickListener {

	private View mView;

	@Override
	public View onCreateView(LayoutInflater inflator, ViewGroup container,
Bundle savedInstanceState) {
		final View view=inflator.inflate(getLayout(), container, false);

		// setup the repeat frequency field
		final TextView header=(TextView)view.findViewById(R.id.
editor_repeat_space_header);
		header.setText(getRepeatHeaderId());

		final EditText period=(EditText)view.findViewById(R.id.
editor_repeat_count);
		period.setText(Integer.toString(getRepeatPeriod()));
		period.addTextChangedListener(this);

		// setup the end date buttons
		final Button endDateBut;
		endDateBut = (Button)view.findViewById(R.id.editor_end_date);
		final int endDate = getArguments().getInt("item_end_date", 0);
		if(endDate>0) {
			GregorianCalendar cal=new GregorianCalendar(
(endDate>>>ItemModel.SHIFT_YEAR)&ItemModel.MASK_YEAR,
(endDate>>>ItemModel.SHIFT_MONTH)&ItemModel.MASK_MONTH,
(endDate>>>ItemModel.SHIFT_DAY)&ItemModel.MASK_DAY);
			endDateBut.setText(DateFormat.getDateInstance().format(cal.getTime()));
		}
		endDateBut.setOnClickListener(this);

		final Button endClear;
		endClear=(Button)view.findViewById(R.id.editor_clear_end);
                endClear.setOnClickListener(this);

		mView=view;
		return view;
	}

	void setArguments(Bundle bundle, String type) {
		super.setArguments(bundle);
		bundle.putString("item_repeat_type", type);
	}

	abstract int getLayout(); // layout Id

	abstract int getRepeatHeaderId(); // string Id

	@Override
	public void onClick(View view) {
		switch(view.getId()) {
		case R.id.editor_end_date:
			buttonEndDate(view);
		break;
		case R.id.editor_clear_end:
			buttonClearEndDate(view);
		break;
		}
	}

	public void buttonEndDate(View view) {
		DatePickerDialog dpd;

		final int val=getArguments().getInt("item_end_date", 0);
		if(val>0) {
			dpd=new DatePickerDialog(getActivity(), this,
(val>>>ItemModel.SHIFT_YEAR)&ItemModel.MASK_YEAR,
(val>>>ItemModel.SHIFT_MONTH)&ItemModel.MASK_MONTH,
(val>>>ItemModel.SHIFT_DAY)&ItemModel.MASK_DAY);
		} else {
			GregorianCalendar cal = new GregorianCalendar();
			dpd=new DatePickerDialog(getActivity(), this,
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
		}

		dpd.show();
	}
	public void onDateSet(DatePicker View, int year, int month, int day) {
		GregorianCalendar cal=new GregorianCalendar(year, month, day);
		Button endDate=(Button)mView.findViewById(R.id.editor_end_date);
		endDate.setText(DateFormat.getDateInstance().format(cal.getTime()));

		getArguments().putInt("item_end_date", year<<ItemModel.SHIFT_YEAR|
month<<ItemModel.SHIFT_MONTH|day<<ItemModel.SHIFT_DAY);
	}
	public void buttonClearEndDate(View view) {
		Button endDate=(Button)mView.findViewById(R.id.editor_end_date);
		endDate.setText("");
		getArguments().putInt("item_end_date", 0);
	}

	@Override
	public void beforeTextChanged(CharSequence s, int a1, int a2, int a3) {
	}
	@Override
	public void onTextChanged(CharSequence s, int a1, int a2, int a3) {
	}

	@Override
	public void afterTextChanged(Editable e) {
		final String str=e.toString();
		if(str.equals("")) return;
		final int val, ret;
		ret=setRepeatPeriod(val=Integer.parseInt(str));
		if(ret!=val) {
			// alert the user to our limits
			final EditText period=(EditText)getView().
findViewById(R.id.editor_repeat_count);
			period.setText(Integer.toString(ret));
		}
	}

	abstract int getRepeatPeriod();

	abstract int setRepeatPeriod(int count);
}

